package main.java.FSMDE.validation.rules;

import java.util.ArrayList;
import java.util.List;

import main.java.FSMDE.models.Component;
import main.java.FSMDE.models.ComponentType;
import main.java.FSMDE.models.Diagram;
import main.java.FSMDE.models.Interface;
import main.java.FSMDE.models.Transition;
import main.java.FSMDE.models.TransitionControl;
import main.java.FSMDE.models.TransitionType;
import main.java.FSMDE.validation.IValidationEntry;
import main.java.FSMDE.validation.rules.IRulesValidator;


public class URLComponentMustReferenceTransitionValidator implements
		IRulesValidator {

	@Override
	public Iterable<IValidationEntry> validate(Diagram diagram) {

		List<IValidationEntry> entries = new ArrayList<IValidationEntry>();
		for(Interface inface : diagram.getInterfaces()){
			for(Component component : inface.getComponents()){
				if(component.getType().equals(ComponentType.URL) ){
					if(! this.hasasosiatedTransition(diagram, component)) entries.add(new URLComponentMustReferenceTransitionValidationEntry(diagram, inface, component));
				}
			}
		}
		
		return entries;
	}

	private boolean hasasosiatedTransition(Diagram diagram, Component component) {
		for(Transition transition : diagram.getTransitions()){
			if(transition.getType().equals(TransitionType.Control)){
				if( ((TransitionControl) transition).getTriggerComponent()!= null && ((TransitionControl) transition).getTriggerComponent().getId() == component.getId()) return true;
			}
			
		}
		return false;
	}

}
